package com.xxxx.microservice.forestry.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xxxx.microservice.forestry.dto.data.Dic;
import com.xxxx.microservice.forestry.dto.param.FeatureAnalysisParam;
import com.xxxx.microservice.forestry.dto.param.InterpolateParam;
import com.xxxx.microservice.forestry.dto.param.QueryCommonParam;
import com.xxxx.microservice.forestry.dto.param.QueryParam;
import com.xxxx.microservice.forestry.dto.result.*;
import com.xxxx.microservice.forestry.selfexception.PredictableException;
import com.xxxx.microservice.forestry.service.ForestryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags="样本查询")
@RestController
@RequestMapping("/forestry")
public class ForestryController {

    @Autowired
    private ForestryService forestryService;


    @ApiOperation(value = "获取行政区树", notes = "市（2位）县（6位）乡（9位）")
    @ApiOperationSupport(order = 1)
    @GetMapping("/getXzqTree")
    public List<Dic> getXzqTree() {
        return forestryService.getXzqTree();
    }

    @ApiOperation(value = "根据行政区编码查询范围", notes = "行政区树的代码")
    @ApiOperationSupport(order = 2)
    @GetMapping("/getXzqRange")
    public String getXzqRange(@RequestParam("xzqdm") String xzqdm) {
        String res = forestryService.getXzqRange(xzqdm);
        if (res == null) {
            throw new PredictableException("未查找到相应行政区");
        }
        return res;
    }

    @ApiOperation(value = "查询所有样本点基本信息", notes = "获取样本点经纬度并在地图上进行展示")
    @ApiOperationSupport(order = 3)
    @GetMapping("/queryAllPoint")
    public List<SamplingPoint> queryAllPoint() {
        return forestryService.queryAllPoint();
    }

    @ApiOperation(value = "样点查询", notes = "查询所有样本点")
    @ApiOperationSupport(order = 4)
    @GetMapping("/query")
    public List<Map<String, Object>> query(QueryCommonParam queryCommonParam) {
        return forestryService.query(queryCommonParam);
    }

    @ApiOperation(value = "查询所有样本点对应属性表", notes = "获取样本点对应属性表")
    @ApiOperationSupport(order = 5)
    @GetMapping("/queryTable")
    public List<Map<String, Object>> queryTable(
            @ApiParam("行政区代码") @RequestParam(value = "xzqdm", required = false) String xzqdm,
            @ApiParam("样点编号")  @RequestParam(value = "ydbh", required = false) String ydbh,
            @ApiParam("表名")  @RequestParam(value = "table", required = false) String table,
            @ApiParam("条件") @RequestParam(value = "filed", required = false) String filed
    ) {
        return forestryService.queryTable(xzqdm, ydbh, table, filed);
    }

    @ApiOperation(value = "查询所有样本点评价结果", notes = "获取样本点对应属性表")
    @ApiOperationSupport(order = 5)
    @GetMapping("/queryPjjg")
    public List<Map<String, Object>> queryPjjg(
            @ApiParam("行政区代码") @RequestParam(value = "xzqdm", required = false) String xzqdm,
            @ApiParam("样点编号")  @RequestParam(value = "ydbh", required = false) String ydbh,
            @ApiParam("表名")  @RequestParam(value = "table", required = false) String table
    ) {
        return forestryService.queryPjjg(xzqdm, ydbh, table);
    }

    @ApiOperation(value = "i查询", notes = "根据样点编号查询样本详细信息")
    @ApiOperationSupport(order = 5)
    @GetMapping("/iQuery")
    public Map<String, Object> iQuery(@ApiParam("样点编号") @RequestParam("ydbh") String ydbh) {
        return forestryService.iQuery(ydbh);
    }

    @ApiOperation(value = "批量导入样本点", notes = "批量导入样本点")
    @ApiOperationSupport(order =6)
    @PostMapping("/insertPoints")
    public int insertPoints(@ApiParam("样点信息") @RequestBody Map<String, Object> points) {
        List<Map<String, Object>> pointList = (List<Map<String, Object>>) points.get("points");
        return forestryService.insertPoints(pointList);
    }

    @ApiOperation(value = "批量导入临时样点", notes = "批量导入临时样点")
    @ApiOperationSupport(order = 7)
    @PostMapping("/pointsTemp")
    public int pointsTemp(@ApiParam("样点信息") @RequestBody Map<String, Object> points) {
        List<Map<String, Object>> pointList = (List<Map<String, Object>>) points.get("points");
        return forestryService.pointsTemp(pointList);
    }

    @ApiOperation(value = "插值分析", notes = "")
    @ApiOperationSupport(order = 8)
    @GetMapping("/interpolate")
    public JSONObject interpolate(InterpolateParam interpolateParam) throws Exception {
        return JSONObject.parseObject(forestryService.interpolate(interpolateParam)) ;
    }

    @ApiOperation(value = "特征分析", notes = "")
    @ApiOperationSupport(order = 9)
    @GetMapping("/featureAnalysis")
    public List<FeatureAnalysisResult> featureAnalysis(FeatureAnalysisParam featureAnalysisParam) {
        return forestryService.featureAnalysis(featureAnalysisParam);
    }

    @ApiOperation(value = "聚类分析", notes = "")
    @ApiOperationSupport(order = 10)
    @GetMapping("/clusterOutlierAnalyst")
    public List<ClusterResult> clusterOutlierAnalyst(
            @ApiParam("行政区代码") @RequestParam(value = "xzqdm", required = false) String xzqdm,
            @ApiParam(value = "字段：有机质（om）、PH（ph）", required = true) @RequestParam(value = "field") String field) {
        return forestryService.clusterOutlierAnalyst(xzqdm, field);
    }

    @ApiOperation(value = "自相关性", notes = "")
    @ApiOperationSupport(order = 10)
    @GetMapping("/autoCorrelation")
    public AutoCorrelationResult autoCorrelation(
            @ApiParam("行政区代码") @RequestParam(value = "xzqdm", required = false) String xzqdm,
            @ApiParam(value = "字段：有机质（om）、PH（ph）", required = true) @RequestParam(value = "field") String field) {
        return forestryService.autoCorrelation(xzqdm, field);
    }
    
    @ApiOperation(value = "空间叠加查询", notes = "")
    @ApiOperationSupport(order = 10)
    @GetMapping("/spatialQuery")
    public List<QueryResult> spatialQuery(
            @ApiParam("查询数据集名称: 土地利用(1),林地小班(2) ") @RequestParam(value = "datasetname", required = true) String datasetname,
            @ApiParam(value = "插值字段取值类型：: 耕地（01）园地（02）林地（03）草地（04）非林地（05）", required = true) @RequestParam(value = "fieldtype") String fieldtype) {
        return forestryService.spatialQuery(datasetname, fieldtype);
    }

    @ApiOperation(value = "地块成果特征分析", notes = "")
    @ApiOperationSupport(order = 14)
    @GetMapping("/polygonAnalysis")
    public List<Map<String, Object>> polygonAnalysis(
            @ApiParam("行政区") @RequestParam(value = "where", required = false) String where,
            @ApiParam(value = "地块类型：ldxb,ditb", required = true) @RequestParam(value = "type") String type,
            @ApiParam(value = "字段：zldwmc, ", required = true) @RequestParam(value = "value") String value,
            @ApiParam(value = "tdly", required = true) @RequestParam(value = "groupBy") String groupBy) {
        return forestryService.polygonAnalysis(where, type, value, groupBy);
    }

    @ApiOperation(value = "样点评价结果统计", notes = "")
    @ApiOperationSupport(order = 14)
    @GetMapping("/pointStatic")
    public List<Map<String, Object>> pointStatic(
            @ApiParam("行政区代码") @RequestParam(value = "xzqdm", required = false) String xzqdm,
            @ApiParam(value = "字段：pollution", required = true) @RequestParam(value = "quotas") String quotas) {
        return forestryService.pointStatic(xzqdm, quotas);
    }

    @ApiOperation(value = "林地小斑评价统计", notes = "")
    @ApiOperationSupport(order = 15)
    @GetMapping("/ldxbAssess")
    public List<Map<String, Object>> ldxbAssess(
            @ApiParam("行政区代码") @RequestParam(value = "xzqdm", required = false) String xzqdm,
            @ApiParam(value = "统计指标：cr_level、qjd_level", required = true) @RequestParam(value = "quotas") String quotas) {
        return forestryService.ldxbAssess(xzqdm, quotas);
    }

    @ApiOperation(value = "地类图斑评价统计", notes = "")
    @ApiOperationSupport(order = 16)
    @GetMapping("/dltbAssess")
    public List<Map<String, Object>> dltbAssess(
            @ApiParam("行政区代码") @RequestParam(value = "xzqdm", required = false) String xzqdm,
            @ApiParam(value = "统计指标：phmean", required = true) @RequestParam(value = "quotas") String quotas) {
        return forestryService.dltbAssess(xzqdm,  quotas);
    }
}
